# -*- coding:utf-8 -*-  

import getpass
import os

#从文件中读取用户名、密码、失败次数列表

file_obj = file('pwd.txt','r')
user_lists = file_obj.readlines()
user_dict = {}
i = 0

while i < len(user_lists):
	user_list = user_lists[i].strip().split(';')
	user_dict[user_list[0]] = {'pwd':user_list[1],'counter':user_list[2]}
	i += 1


#输入用户名和密码
print "please input user name：\n"
user_name = raw_input()

if user_name not in user_dict.keys():
	print "wrong user name!"
	os._exit(0)

#进行比较
#如果用户名存在，比较失败次数，如果失败次数大于等于3，提示用户已锁定
#如果密码正确，提示登录成功；如果密码错误，提示密码错误，失败次数加1
count = int(user_dict[user_name]['counter'])

if count > 2:
	print "the user is locked"
	os._exit(0)


while count < 3:
	print "please input password:\n"
	pwd = getpass.getpass()
	if pwd != user_dict[user_name]['pwd']:
		print "wrong password!"
		count += 1
		continue
	else:
		print "login success"
		break

user_dict[user_name]['counter'] = str(count)
new_str = []

for key,value in user_dict.items():
	new_str.append( ";".join([key,value['pwd'],value['counter']]))
	print new_str

new_str2 = "\n".join(new_str)



